import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserInfoService } from '@/api/user'

// 用户模块 token setToken removeToken
export const useUserStore = defineStore(
  'News-user',
  () => {
    //创建token 的ref
    const token = ref('')
    const setToken = (newToken) => {
      token.value = newToken
    }
    const removeToken = () => {
      token.value = ''
    }
    // 创建个人信息的ref
    const user = ref({})
    const getUserInfo = async () => {
      const res = await getUserInfoService()
      user.value = res.data.data
    }
    const setUserInfo = (obj) => (user.value = obj)
    const resetUserInfo = () => {
      user.value = {}
    }

    return {
      token,
      setToken,
      removeToken,
      user,
      getUserInfo,
      setUserInfo,
      resetUserInfo
    }
  },
  {
    persist: true
  }
)
